package chapter9.rent.service;

import chapter9.rent.domain.House;

/*1.定义House数组，保存House对象
  2.相应HouseView的调用
  3.完成对房屋信息的各种操作crud
* */

public class HouseService {
    private House[] houses;//保存House对象
    private int HouseNum = 1;//用来记录当前有多少房屋信息
    private int idCounter = 1;//用来记录当前Id增长到哪个值了

    public HouseService(int size) {
        //new houses
        houses = new House[size];//当创建HOuseService对象，指定数组大小
        houses[0] = new House(1, "232", "13123", "海淀区", 100, "已经出租");
    }

    public House[] list() {
        return houses;
    }

    //add方法，添加新对象,返回boolean
    public boolean add(House newHouse) {
        //先判断数组是否满了
        //考虑数组扩容机制
        if (HouseNum == houses.length) {
            System.out.println("数组已满，不能在添加了");
            return false;
        }
        //把newHouse对象加入到房屋列表信息

        houses[HouseNum++] = newHouse;
        //设计一个Id自增长的机制
        newHouse.setId(++idCounter);
        return true;
    }

    public boolean delete(int id) {
        //先确定数组中是否存在，为该ID的房屋信息
        int index = -1;//存储要删除的下标
        for (int i = 0; i < HouseNum; i++) {
            if (houses[i].getId() == id) {
                index = i;//记录下标
            }
        }
        if (index == -1) {
            System.out.println("不存在该ID的房屋信息");
            return false;
        }
        for (int i = index; i < HouseNum - 1; i++) {
            houses[i] = houses[i + 1];//用后边的元素覆盖前面的
        }
        houses[--HouseNum] = null;//将存在的最后一个设置为null
        return true;
    }

    public House find(int id) {
        //根据id查找房屋信息
        int index = -1;//记录下标
        for (int i = 0; i < HouseNum; i++) {
            if (houses[i].getId() == id){
                index= i;//如果id相等就记录下标值
            }
        }
        if (index == -1){
            return null;
        }

        return houses[index];
    }

    public House modify(House house,String name , String phone ,String address,int rent,String state){
        if (name.equals("")){

        }
        else{
            house.setOwner(name);
        }
        if (phone.equals("")){

        }
        else{
            house.setPhoneNumber(phone);
        }
        if (address.equals("")){

        }
        else{
            house.setAddress(address);
        }
        if (rent == house.getRent()){

        }
        else{
            house.setRent(rent);
        }
        if (state.equals("")){
         //如果输入空串就还是原来的值
        }
        else{
            house.setState(state);
        }
        return house;
    }
}
